<?php

namespace App\Providers;

use App\Http\Controllers\CorsController;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    protected $app_namespace = 'App\Http\Controllers\App';

    protected $common_namespace = 'App\Http\Controllers\Common';

    protected $promo_namespace = 'App\Http\Controllers\MayPromo';

    protected $admin_namespace = 'App\Http\Controllers\Admin';

    protected $pc_namespace = 'App\Http\Controllers\PC';

    protected $wechat_namespace = 'App\Http\Controllers\Wechat';

    protected $open_wechat_namespace = 'App\Http\Controllers\OpenWechat';

    protected $youzan_namespace = 'App\Http\Controllers\Youzan';

    protected $debug_namespace = 'App\Http\Controllers\Debug';

    protected $service_namespace = 'App\Http\Controllers\Service';

    protected $service_visit_namespace = 'App\Http\Controllers\Service\Visit';

    protected $service_util_namespace = 'App\Http\Controllers\Service\Util';

    // 开放平台的控制器的命名空间
    protected $openflatform_namespace = 'App\Http\Controllers\open';
    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //
        $this->initMacros();

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiPromoRoutes();

        $this->mapApiV1Routes();

        $this->mapApiV2_3Routes();

//        $this->mapApiV2_3_1_10Routes();

        $this->mapApiV3_1Routes();

        $this->mapWebRoutes();

        $this->mapOpenWechatRoutes();

        $this->mapYouzanRoutes();

        $this->mapDebugRoutes();

        $this->mapServiceRoutes();

        $this->mapOpenPlatformRoutes();
    }

    /**
     * 魔妆镜开放平台的路由
     */
    protected function mapOpenPlatformRoutes()
    {
        Route::prefix('open')
            ->middleware('api')
            ->namespace($this->openflatform_namespace)
            ->group(base_path('routes/open/api/open.php'));
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiV1Routes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));

        Route::prefix('api/app')
             ->middleware(['api', 'AppAuth'])
             ->namespace($this->app_namespace)
             ->group(base_path('routes/api/v1/app.php'));

        Route::prefix('api/app-web-view')
            ->middleware(['Cors','api'])
            ->namespace($this->app_namespace)
            ->group(base_path('routes/api/v1/app_web_view.php'));

        Route::prefix('api/common')
            ->middleware(['Cors','api'])
            ->namespace($this->common_namespace)
            ->group(base_path('routes/api/v1/common.php'));

        Route::prefix('api/admin')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api/v1/admin/customer.php'));

        Route::prefix('api/admin')
            ->middleware(['api', 'AppLocale'])
            ->namespace($this->admin_namespace)
            ->group(base_path('routes/api/v1/admin/auth.php'));

        Route::prefix('api/admin')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api/v1/admin/panx.php'));

        Route::prefix('api/admin')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api/v1/admin/store.php'));

        Route::prefix('api/admin')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api/v1/admin/service.php'));

        Route::prefix('api/pc')
            ->middleware(['api', 'AppLocale'])
            ->namespace($this->pc_namespace)
            ->group(base_path('routes/api/v1/pc.php'));

        Route::prefix('api/wechat')
            ->middleware('api')
            ->namespace($this->wechat_namespace)
            ->group(base_path('routes/api/v1/wechat.php'));
    }

    protected function mapApiV2_3Routes()
    {
        Route::prefix('api/app')
            ->middleware(['api', 'AppAuth'])
            ->namespace($this->app_namespace)
            ->group(base_path('routes/api/v2_3/app.php'));


        Route::prefix('api/wechat')
            ->middleware('api')
            ->namespace($this->wechat_namespace)
            ->group(base_path('routes/api/v2_3/wechat.php'));
    }

    protected function mapApiV3_1Routes()
    {
        Route::prefix('api/wechat')
            ->middleware('api')
            ->namespace($this->wechat_namespace)
            ->group(base_path('routes/api/v3_1/wechat.php'));
    }

    protected function mapApiV2_3_1_10Routes()
    {

        Route::prefix('api/admin')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api/v2_3_1_10/admin/customer.php'));

        Route::prefix('api/admin')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api/v2_3_1_10/admin/store.php'));
    }

    protected function mapApiPromoRoutes()
    {

        Route::prefix('api/may-promo')
            ->middleware(['api'])
            ->namespace($this->promo_namespace)
            ->group(base_path('routes/api/may-promo/promo.php'));

    }


    /**
     * Define the "open wechat" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapOpenWechatRoutes()
    {
        Route::prefix('open-wechat')
            ->namespace($this->open_wechat_namespace)
            ->group(base_path('routes/open-wechat/server.php'));

        Route::prefix('open-wechat')
            ->namespace($this->open_wechat_namespace)
            ->group(base_path('routes/open-wechat/authorizer.php'));

        Route::middleware(['web', 'Cors'])
            ->prefix('open-wechat')
            ->namespace($this->open_wechat_namespace)
            ->group(base_path('routes/open-wechat/oauth.php'));
    }

    protected function mapYouzanRoutes()
    {
        Route::middleware(['web', 'Cors'])
            ->prefix('youzan')
            ->namespace($this->youzan_namespace)
            ->group(base_path('routes/youzan/oauth.php'));

    }

    private function mapDebugRoutes()
    {
        if (! config('pkx.route_debug')) return;

        Route::middleware(['web'])
            ->prefix('debug')
            ->namespace($this->debug_namespace)
            ->group(base_path('routes/debug/debug.php'));
    }

    private function mapServiceRoutes()
    {
        Route::middleware(['api'])
            ->prefix('api/service')
            ->namespace($this->service_namespace)
            ->group(base_path('routes/api/service/service.php'));

        Route::middleware(['api'])
            ->prefix('api/service/visit')
            ->namespace($this->service_visit_namespace)
            ->group(base_path('routes/api/service/visit.php'));

        Route::middleware(['api'])
            ->prefix('api/service/util')
            ->namespace($this->service_util_namespace)
            ->group(base_path('routes/api/service/util.php'));
    }

    /**
     * 绑定路由宏
     */
    public function initMacros()
    {
        /**
         * 跨域预检路由宏
         *
         * @see CorsController::preFlightCheck()
         */
        Route::macro('corsAny', function ($uri = '/') {
            Route::options($uri . '{any}', '\\App\\Http\\Controllers\\CorsController@preFlightCheck')->where('any', '.*');
        });
    }
}
